﻿using Inovout.Kuajing.Lottery.Models;
using Inovout.Kuajing.Lottery.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Lottery.WebSite.Controllers
{
    public class LotteryController : KJBaseController<Active>
    {
        private IActiveService activeService;
        private IRedPacketService redPacketService;
        private LotteryService lotteryService;

        public LotteryController()
        {
 
        }

        public LotteryController(IActiveService activeService, IRedPacketService redPacketService)
        {
            this.activeService = activeService;
            this.redPacketService = redPacketService;
            lotteryService = new LotteryService();

        }

        [HttpGet]
        public ActionResult Index(int  activeId)
        {
            string userCode = Thread.CurrentPrincipal.Identity.Name;

            //活动是否已停止
            Active currenActive = activeService.FindById(activeId);
            if (!currenActive.Enabled)
            {
                return RedirectToAction("Fail", new  { result = "活动已结束" }); 
            }

            //更新用户点击数
            bool is_NewUser=false;
            //if (!lotteryService.UpdateUserClick(userCode, currenActive,out is_NewUser))
            //{
            //    return RedirectToAction("Fail", new { result = "点击数已过" }); 
            //}

            RedPacket prize = null;
            //获取红包
            if (!lotteryService.GetRedPacket(userCode, currenActive, out prize, is_NewUser))
            {
                return RedirectToAction("Fail", new { result = "活动已结束" });
            }
            

            if (prize != null)
            {
                //摇中奖
                if (prize.Category == 1) 
                {
                    TempData["RedPacket"] = prize;
                    ViewBag.UserCode = userCode;
                    return RedirectToAction("ReceivePrize", new { userCode = userCode });
                }

                //空门
                if (prize.Category == 0)
                {
                    return RedirectToAction("Continue", new  { activeId = activeId });
                }
            }
            return View(prize);
        }

        [HttpGet]
        public ActionResult ReceivePrize(string userCode)
        {
            RedPacket redPacket = (RedPacket)TempData["RedPacket"];
            ViewBag.UserCode = userCode;
            return View(redPacket);
        }


        [HttpPost]
        public ActionResult ReceivePrize(string userCode, RedPacket redPacket)
        {
            lotteryService.SaveWinnersList(userCode, redPacket);
            TempData["RedPacket"] = redPacket;
            return RedirectToAction("WinPrize");
        }

        public ActionResult Fail(string result)
        {
            ViewBag.Result = result;
            return View();
        }

        public ActionResult Continue(string activeId)
        {
            ViewBag.ActiveId = activeId;
            return View();
        }

        public ActionResult WinPrize()
        {
            RedPacket redPacket = (RedPacket)TempData["RedPacket"];
            ViewBag.ActiveId = (int)redPacket.Active.Id;
            return View(redPacket);
        }
       
    }
}